<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Album extends CI_Controller
{

    /**
     * 判断用户是否登录
     *
     * @access public
     * @return void
     */
    public function __construct()
    {
        parent::__construct();

        $this->load->model('model_member', 'member');

        $this->member->init();
        if ( ! $this->member->is_login())
        {
            $this->member->goto_login();
        }

        $this->load->model('model_album', 'album');
    }

    /**
     * 我的相册
     *
     * @access public
     * @return void
     */
	public function index()
	{
        $rowset = $this->album->my($this->member->uid);

        $this->load->view('album_index', array('rowset' => $rowset));
	}

    /**
     * 创建相册
     *
     * @access public
     * @return void
     */
	public function add()
	{
        $this->load->view('album_add');
	}

    /**
     * 更新相册
     *
     * @access public
     * @return void
     */
	public function update()
	{
        $rows = $this->album->get($this->input->get('id'));
        if (empty($rows) || $rows['album_uid'] != $this->member->uid)
        {
            show_error('您请求的相册不存在！');
        }
        $rows['album_weibo'] = explode(',', $rows['album_weibo']);

        $this->load->view('album_update', array('rows' => $rows));
	}

    /**
     * 删除相册
     *
     * @access public
     * @return void
     */
    public function remove()
    {
        $rows = $this->album->get($this->input->post('id'));
        if (empty($rows) || $rows['album_uid'] != $this->member->uid)
        {
            $response = array('error_code' => TRUE, 'msg' => '您请求的相册不存在！');
        }
        else
        {
            $this->album->remove($rows['album_id']);
            $response = array('error_code' => FALSE, 'msg' => 'ok');
        }

        echo json_encode($response);
    }

    /**
     * 保存相册
     *
     * @access public
     * @return void
     */
    public function save()
    {
        $response = $this->album->save($this->member->uid);

        echo json_encode($response);
    }

    /**
     * 上传图片
     *
     * @access public
     * @return void
     */
	public function picture()
	{
        $rows = $this->album->get($this->input->get('id'));
        if (empty($rows) || $rows['album_uid'] != $this->member->uid)
        {
            show_error('您请求的相册不存在！');
        }

        $rows['album_picture'] = empty($rows['album_picture']) ? array() : explode(',', $rows['album_picture']);

        $this->load->view('album_picture', array('rows' => $rows));
	}

    /**
     * 保存上传图片
     *
     * @access public
     * @return void
     */
    public function upload()
    {
        $rows = $this->album->get($this->input->get('id'));
        if (empty($rows) || $rows['album_uid'] != $this->member->uid)
        {
            $response = array('error_code' => TRUE, 'msg' => '您请求的相册不存在！');
        }
        else
        {
            // 图片总数
            $total = substr_count($rows['album_picture'], ',');
            $max   = $this->config->item('album_max_picture');
            if ($total >= ($max - 1))
            {
                $response = array('error_code' => TRUE, 'msg' => '每个相册最多上传' . $max . '张图片！');
            }
            else
            {
                $response = $this->album->upload($rows['album_id']);
                
                $this->album->save_picture($rows['album_id'], $rows['album_picture'], $response['data']['file_name']);
            }
        }

        echo json_encode($response);
    }

    /**
     * 删除上传的图片
     *
     * @access public
     * @return void
     */
    public function delete()
    {
        $rows = $this->album->get($this->input->get('id'));
        if (empty($rows) || $rows['album_uid'] != $this->member->uid)
        {
            $response = array('error_code' => TRUE, 'msg' => '您请求的相册不存在！');
        }
        else
        {
            $this->album->delete_picture($rows['album_id'], $rows['album_picture'], $this->input->get('name'));
            $response = array('error_code' => FALSE, 'msg' => 'ok');
        }

        echo json_encode($response);
    }

    public function generate()
    {
        $rows = $this->album->get($this->input->get('id'));
        if (empty($rows) || $rows['album_uid'] != $this->member->uid)
        {
            $response = array('error_code' => TRUE, 'msg' => '您请求的相册不存在！');
        }
        else
        {
            $this->album->generate($rows['album_id']);
            $response = array('error_code' => FALSE, 'msg' => 'ok');
        }

        echo json_encode($response);
    }

    /**
     * 管理相册
     *
     * @access public
     * @return void
     */
    public function manage()
    {
        if ( ! $this->member->is_admin())
        {
            show_error('您没有权限！');
        }

        $do = $this->input->get('do');
        if (empty($do))
        {
            $rowset = $this->album->my(0, 1);

            $this->load->view('album_manage', array('rowset' => $rowset));
        }
        elseif ($do == 'remove')
        {
            $this->album->remove($this->input->post('id'));
            $response = array('error_code' => FALSE, 'msg' => 'ok');

            echo json_encode($response);
        }
    }

    /**
     * 微博时间线
     *
     * @access public
     * @return void
     */
    public function timeline()
    {
        $response = $this->album->timeline($this->member->uid, $this->input->get('id'), $this->input->get('page'));

        echo json_encode($response);
    }
}